<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends JS_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('users_mdl');
	}

	public function index(){
		if($this->session->suser->group != 1){
			show_error('参数错误！');
		}

		$this->db->start_cache();
		//查询用户权限组
		$url = site_url('users/index').'?';
		$level = $this->input->get('level',TRUE);
		if(!empty($level)){
			$url .= 'level='.$level.'&';
			if($level == 'user'){
				$level = 0;
			}else{
				$level = intval($level);
			}
			$this->db->where('level_id',$level);
		}

		$words = $this->input->get('words',TRUE);
		if(!empty($words)){
			$this->db->like('username',$words);
			$this->db->or_like('user_name',$words);
			$url .= 'words='.$words.'&';
		}

		$pid = $this->input->get('pid',TRUE);
		$pid = intval($pid);
		if(!empty($pid)){
			$this->db->where('user_pid',$pid);
			$url .= 'pid='.$pid.'&';
		}

		$this->db->where('user_id !=',0);

		$this->db->stop_cache();

		$this->load->library('pagination');

        $config['base_url'] = mb_substr($url,0,-1);
        $config['total_rows'] = $this->users_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';
        $config['num_links'] = 3;

        $this->pagination->initialize($config);
        
        $page = $this->pagination->create_links();
		
		$per_page = $this->input->get('per_page',TRUE);
		$this->db->order_by('user_id desc');
		$this->db->limit($config['per_page'],intval($per_page));
		$list = $this->users_mdl->select();

		$this->db->flush_cache();
		//组装数据
		$list = $this->users_mdl->restr($list);

		//查询权限
		//查询权限
		$this->load->model('level_mdl');
		$this->db->where('group_id !=',0);
		$level_list = $this->level_mdl->select();
		$data = [
			'page' => $page,
			'list' => $list,
			'level' => $level_list
		];

		$this->_view('users_index',$data);

	}

	/**
	 * 查看下辖所有用户
	 */
	public function get_lower(){
		$res_list = $this->users_mdl->get_lower($this->session->suser->user_id);
		$map = [];

		foreach($res_list as $k => $v){
			$map[] = $v['user_id'];
		}


		//组装查询条件
		$this->db->start_cache();
		$this->db->where_in('user_id',$map);

		$url = site_url('users/index').'?';
		$level = $this->input->get('level',TRUE);
		if(!empty($level)){
			$url .= 'level='.$level.'&';
			if($level == 'user'){
				$level = 0;
			}else{
				$level = intval($level);
			}
			$this->db->where('level_id',$level);
		}

		$words = $this->input->get('words',TRUE);
		if(!empty($words)){
			$this->db->like('username',$words);
			$this->db->or_like('user_name',$words);
			$url .= 'words='.$words.'&';
		}

		$pid = $this->input->get('pid',TRUE);
		$pid = intval($pid);
		if(!empty($pid)){
			$this->db->where('user_pid',$pid);
			$url .= 'pid='.$pid.'&';
		}


		$this->db->stop_cache();

		$this->load->library('pagination');

        $config['base_url'] = mb_substr($url,0,-1);
        $config['total_rows'] = $this->users_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';
        $config['num_links'] = 3;

        $this->pagination->initialize($config);
        
        $page = $this->pagination->create_links();
		
		$per_page = $this->input->get('per_page',TRUE);
		$this->db->order_by('user_id desc');
		$this->db->limit($config['per_page'],intval($per_page));
		$list = $this->users_mdl->select();

		$this->db->flush_cache();
		//组装数据
		$list = $this->users_mdl->restr($list);

		//查询权限
		$this->load->model('level_mdl');
		$this->db->where('group_id',$this->session->suser->level_id);
		$level = $this->level_mdl->get_one();

		$this->db->where('hierarchy >',(int)$level['hierarchy']);
		$this->db->where('group_id !=',0);
		$level_list = $this->level_mdl->select();
		$data = [
			'page' => $page,
			'list' => $list,
			'level' => $level_list
		];

		$this->_view('users_lower',$data);
	}


	/**
	 * 服务器用户数据同步
	 */
	public function synchronous(){
		if(!$this->input->is_ajax_request()){
			show_404();
		}

		//查询数据库已有数据
		$this->db->select('user_id');
		$res_user = $this->users_mdl->select();

		$user_id = '';
		foreach($res_user as $k => $v){
			$user_id .= $v['user_id'].',';
		}

		$url = 'http://zbzmall.com/api/getall?key='.$this->get_key().'&user_id='.substr($user_id,0,-1);

		$res = $this->get_cunt($url);

		if(empty($res)){
			die('已经是最新数据，不需要更新');
		}

		//将数据插入数据库
		$result = '';
		//print_r($res);exit;
		foreach($res as $k => $v){
			$res = $this->users_mdl->create((array)$v);

			if($res === false){
				$result .= '用户：'.$v->username.' 同步失败！<br />';
			}
		}

		echo empty($result) ? '1' : $result;
	}


}

/* End of file Users.php */
/* Location: ./application/controllers/Users.php */